{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Decrypt String from Alphabet to Integer Mapping"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: freqAlphabets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #解码字母到整数映射"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串&nbsp;<code>s</code>，它由数字（<code>'0'</code> - <code>'9'</code>）和&nbsp;<code>'#'</code>&nbsp;组成。我们希望按下述规则将&nbsp;<code>s</code>&nbsp;映射为一些小写英文字符：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>字符（<code>'a'</code> - <code>'i'</code>）分别用（<code>'1'</code> -&nbsp;<code>'9'</code>）表示。</li>\n",
    "\t<li>字符（<code>'j'</code> - <code>'z'</code>）分别用（<code>'10#'</code>&nbsp;-&nbsp;<code>'26#'</code>）表示。&nbsp;</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回映射之后形成的新字符串。</p>\n",
    "\n",
    "<p>题目数据保证映射始终唯一。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"10#11#12\"\n",
    "<strong>输出：</strong>\"jkab\"\n",
    "<strong>解释：</strong>\"j\" -&gt; \"10#\" , \"k\" -&gt; \"11#\" , \"a\" -&gt; \"1\" , \"b\" -&gt; \"2\".\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"1326#\"\n",
    "<strong>输出：</strong>\"acz\"\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 1000</code></li>\n",
    "\t<li><code>s[i]</code> 只包含数字（<code>'0'</code>-<code>'9'</code>）和&nbsp;<code>'#'</code>&nbsp;字符。</li>\n",
    "\t<li><code>s</code>&nbsp;是映射始终存在的有效字符串。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [decrypt-string-from-alphabet-to-integer-mapping](https://leetcode.cn/problems/decrypt-string-from-alphabet-to-integer-mapping/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [decrypt-string-from-alphabet-to-integer-mapping](https://leetcode.cn/problems/decrypt-string-from-alphabet-to-integer-mapping/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"10#11#12\"', '\"1326#\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans, i = '', len(s) -1\n",
    "        while i >= 0:\n",
    "            if s[i] == '#':\n",
    "                ans += chr(int(s[i-2:i]) + 96)\n",
    "                i -= 3\n",
    "            else:\n",
    "                ans += chr(int(s[i]) + 96)\n",
    "                i -= 1\n",
    "        return ans[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2] == '#':\n",
    "                ans += get(s[i : i + 2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        s = s[::-1]\n",
    "        res = ''\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] != '#':\n",
    "                res += chr(int(s[i])+96)\n",
    "                i += 1\n",
    "            else:\n",
    "                res += chr(int(s[i+2]+s[i+1])+96)\n",
    "                i += 3\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        stack=[]\n",
    "        for i in s:\n",
    "            if not stack or i!=\"#\":\n",
    "                stack.append(i)\n",
    "            else:\n",
    "                p2=stack.pop()\n",
    "                p1=stack.pop()\n",
    "                stack.append(p1+p2)\n",
    "        return \"\".join([chr(ord(\"a\")-1+int(i)) for i in stack])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        res = []\n",
    "        while i < n:\n",
    "            if i <= n - 3 and s[i + 2] == '#':\n",
    "                num = int(s[i: i+2])\n",
    "                c = chr(ord('a') + num - 1)\n",
    "                res.append(c)\n",
    "                i += 3\n",
    "            else:\n",
    "                num = int(s[i])\n",
    "                c = chr(ord('a') + num - 1)\n",
    "                res.append(c)\n",
    "                i += 1\n",
    "        return ''.join(res)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        i = 0\n",
    "        res = \"\"\n",
    "        while i<len(s):\n",
    "            if i+2<len(s) and s[i+2]=='#':\n",
    "                res += chr(int(s[i:i+2])+96)\n",
    "                i += 2\n",
    "            else:\n",
    "                res += chr(int(s[i])+96)\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans = []\n",
    "        strs = s.split('#')\n",
    "        for s in strs[:-1]:\n",
    "            if len(s)>=2:\n",
    "                for i in range(len(s)-2):\n",
    "                    ans.append(chr(ord('a') + int(s[i]) -1))\n",
    "                ans.append(chr(ord('a') + int(s[-2:]) -1))\n",
    "            else:\n",
    "                ans.append(chr(ord('a') + int(s) -1))\n",
    "        for i in range(len(strs[-1])):\n",
    "            ans.append(chr(ord('a') + int(strs[-1][i]) -1))\n",
    "        return \"\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        a = {'1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i'}\n",
    "        b = {'10#': 'j', '11#': 'k', '12#': 'l', '13#': 'm', '14#': 'n', '15#': 'o', '16#': 'p', '17#': 'q',\n",
    "              '18#': 'r', '19#': 's', '20#': 't', '21#': 'u', '22#': 'v', '23#': 'w', '24#': 'x', '25#': 'y',\n",
    "              '26#': 'z'}\n",
    "        k = len(s)\n",
    "        i = 0\n",
    "        ans = \"\"\n",
    "        while i<k:\n",
    "            if i+2<k and s[i+2] == \"#\":\n",
    "                ans += b[s[i:i+3]]\n",
    "                i +=3\n",
    "            else:\n",
    "                ans += a[s[i]]\n",
    "                i += 1\n",
    "            \n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st)+96)\n",
    "        \n",
    "        i,ans =0, \"\"\n",
    "        while i <len(s):\n",
    "            if i+2 <len(s) and s[i+2] == '#':\n",
    "                ans +=get(s[i : i+2])\n",
    "                i +=2\n",
    "            else:\n",
    "                ans +=get(s[i])\n",
    "            i +=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        return ''.join(chr(int(i[:2]) + 96) for i in re.findall(r'\\d\\d#|\\d', s))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        return ''.join(chr(int(i[:2]) + 96) for i in re.findall(re.compile(r'\\d\\d#|\\d'), s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        idx, n = 0, len(s)\n",
    "        ans = \"\"\n",
    "        while idx < n:\n",
    "            if idx + 2 < n and s[idx+2] == '#':\n",
    "                ans += chr(96 + int(s[idx:idx+2]))\n",
    "                idx += 2  \n",
    "            else:\n",
    "                ans += chr(96 + int(s[idx]))\n",
    "            idx += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        i=len(s)-1\n",
    "        ans=\"\"\n",
    "        while i>=0:\n",
    "            if s[i]==\"#\":\n",
    "                ans=chr(96+int(s[i-2:i]))+ans\n",
    "                i=i-3\n",
    "            else:\n",
    "                ans=chr(96+int(s[i]))+ans\n",
    "                i=i-1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans, i = '', len(s) - 1\n",
    "        while i >= 0:\n",
    "            if s[i] == '#':\n",
    "                ans += chr(int(s[i - 2: i]) + 96)\n",
    "                i -= 3\n",
    "            else:\n",
    "                ans += chr(int(s[i]) + 96)\n",
    "                i -= 1\n",
    "        return ans[:: -1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        ans = ''\n",
    "        key = string.ascii_lowercase\n",
    "        i = n-1\n",
    "        while i>=0:\n",
    "            if s[i] == '#':\n",
    "                i -=2\n",
    "                t = key[int(s[i:i+2])-1]\n",
    "                ans += t\n",
    "            else:\n",
    "                t = key[int(s[i])-1]\n",
    "                ans += t\n",
    "            i -=1\n",
    "\n",
    "        return ans[::-1]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        idx = 0\n",
    "        n = len(s)\n",
    "        res = ''\n",
    "        while idx < n:\n",
    "            if (idx+3) > n:\n",
    "                res += chr(int(s[idx])+96)\n",
    "                idx += 1\n",
    "            else:\n",
    "                if s[idx+2] == \"#\":\n",
    "                    res += chr(int(s[idx:idx+2])+96)\n",
    "                    idx += 3\n",
    "                else:\n",
    "                    res += chr(int(s[idx])+96)\n",
    "                    idx += 1\n",
    "        return res\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st)+96)\n",
    "        i,ans=0,''\n",
    "        while i<len(s):\n",
    "            if i+2<len(s) and s[i+2]=='#':\n",
    "                ans+=get(s[i:i+2])\n",
    "                i+=2\n",
    "            else:\n",
    "                ans+=get(s[i])\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        reversed_arr = []\n",
    "        c = 0\n",
    "        x = ''\n",
    "        for ch in reversed(s):\n",
    "            if ch == '#':\n",
    "                c = 2\n",
    "                x = ''\n",
    "                continue \n",
    "            if c != 0:\n",
    "                x = ch + x \n",
    "                c -= 1\n",
    "            else:\n",
    "                x = ch\n",
    "\n",
    "            if c == 0:\n",
    "                reversed_arr.append(x)\n",
    "        arr = list(reversed(reversed_arr))\n",
    "        ans = [chr(int(a) - 1 + ord('a')) for a in arr]\n",
    "        ans = \"\".join(ans)\n",
    "        return ans \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        if not s:\n",
    "            return \"\"\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        ans = \"\"\n",
    "        while i < n:\n",
    "            if i + 2 < n and s[i + 2] == \"#\":\n",
    "                ans += chr(int(s[i:i+2])+96)\n",
    "                i += 3\n",
    "            else:    \n",
    "                ans += chr(int(s[i]) + 96)\n",
    "                i += 1\n",
    "\n",
    "        return ans            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        N = len(s)\n",
    "        toAlpha = lambda n: chr(n-1+ord('a')) \n",
    "        \n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i < N:\n",
    "            if i+2 < N and s[i+2]==\"#\":\n",
    "                ans.append(toAlpha(int(s[i:i+2])))\n",
    "                i += 3\n",
    "            else:\n",
    "                ans.append(toAlpha(int(s[i])))\n",
    "                i += 1\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2] == '#':\n",
    "                ans += get(s[i : i + 2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ss=s.split(\"#\")\n",
    "        ans=\"\"\n",
    "        for i in range(len(ss)):\n",
    "            temp=ss[i]\n",
    "            if temp!=\"\":\n",
    "                if len(temp)>2:\n",
    "                    for j in range(0,len(temp)-2):\n",
    "                        ans+=chr((ord(\"a\") + int(temp[j])-1))\n",
    "                    ans+=chr(ord('j')+int(temp[-2:])-10)\n",
    "                elif len(temp)==2:\n",
    "                    # print(temp[-2:])\n",
    "                    if i==len(ss)-1:\n",
    "                        # print(s[-1])\n",
    "                        if s[-1]!=\"#\":\n",
    "                            ans+=chr(ord('a') +int(temp[0])-1)\n",
    "                            ans+=chr(ord('a') +int(temp[1])-1)\n",
    "                        else:\n",
    "                            ans+=chr(ord('j')+int(temp[-2:])-10)                        \n",
    "                    else:\n",
    "                        ans+=chr(ord('j')+int(temp[-2:])-10)\n",
    "                else:\n",
    "                    ans+=chr(ord('a') +int(temp[0])-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2] == '#':\n",
    "                ans += get(s[i : i + 2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2] == '#':\n",
    "                ans += get(s[i : i + 2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        mp = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        index = 0\n",
    "        s = s[::-1]\n",
    "        res = \"\"\n",
    "        while index< len(s):\n",
    "            l = s[index]\n",
    "            if l == '#':\n",
    "                d = int(s[index+2:index:-1])\n",
    "                index += 3\n",
    "            else:\n",
    "                d = int(l)\n",
    "                index += 1\n",
    "            # print(d)\n",
    "            res  = res+mp[d-1]\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        res = []\n",
    "        letters ='@abcdefghijklmnopqrstuvwxyz'\n",
    "        for c in range(len(s)):\n",
    "            if s[c] != '#':\n",
    "                res.append(letters[int(s[c])])\n",
    "            else:\n",
    "                res= res[:-2]\n",
    "                res.append(letters[int(s[c-2:c])])\n",
    "        return ''.join(res)\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        i = n - 1\n",
    "        ans = ''\n",
    "        while i >= 0:\n",
    "            if s[i] == '#':\n",
    "                tmp = s[i-2:i]\n",
    "                i = i - 3\n",
    "            else:\n",
    "                tmp = s[i]\n",
    "                i = i - 1\n",
    "            ans = ans + chr(int(tmp)-1+97)\n",
    "\n",
    "        ans = ans[::-1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        dict1 = {k:chr(ord('a')+k-1) for k in range(1,10)}\n",
    "        dict2 = {str(k)+'#':chr(ord('j')+k-10) for k in range(10,27)}\n",
    "        def tihuan1(s):\n",
    "            s = s.group(0)\n",
    "            return dict2[s]\n",
    "        def tihuan2(s):\n",
    "            s = int(s.group(0))\n",
    "            return dict1[s]\n",
    "        s = re.sub('\\d\\d#',tihuan1,s)\n",
    "        return re.sub('\\d',tihuan2,s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        counter=dict()\n",
    "        for i in range(1,10):\n",
    "            counter[str(i)]=chr(ord('a')+i-1)\n",
    "        for i in range(10,27):\n",
    "            counter[str(i)+'#']=chr(ord('j')+i-10)\n",
    "        j=0\n",
    "        n=len(s)\n",
    "        res=''\n",
    "        while j+2<n:\n",
    "            if s[j+2]=='#':\n",
    "                res+=counter[s[j:j+3]]\n",
    "                j+=3\n",
    "            else:\n",
    "                res+=counter[s[j:j+1]]\n",
    "                j+=1\n",
    "        if j==n-2:\n",
    "            res+=counter[s[j]]+counter[s[j+1]]\n",
    "        if j==n-1:\n",
    "            res+=counter[s[j]]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2] == '#':\n",
    "                ans += get(s[i: i + 2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        l = []\n",
    "        cur = len(s) - 1\n",
    "        while cur >= 0:\n",
    "            if s[cur] == '#':\n",
    "                l.append(chr(int(s[cur-2:cur])+96))\n",
    "                cur -= 3\n",
    "            else:\n",
    "                l.append(chr(int(s[cur])+96))\n",
    "                cur -= 1\n",
    "        l.reverse()\n",
    "        return ''.join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        i, j = 0, 1\n",
    "        word = {\n",
    "            \"1\": \"a\",\n",
    "            \"2\": \"b\", \"3\": \"c\", \"4\": \"d\", \"5\": \"e\", \"6\":\"f\",\n",
    "            \"7\": \"g\", \"8\": \"h\", \"9\": \"i\", \"10\": 'j', \"11\": 'k',\n",
    "            '12': 'l', '13': 'm', '14': 'n', '15': 'o', '16': 'p',\n",
    "            '17': 'q', '18': 'r', '19': 's', '20': 't', '21': 'u',\n",
    "            '22': 'v', '23': 'w', '24': 'x', '25': 'y', '26': 'z' \n",
    "        }\n",
    "        ans = []\n",
    "        while i < len(s):\n",
    "            temp = s[i:j]\n",
    "            if s[i] == '1' or s[i] == '2':\n",
    "                # 说明可能是带#的，找到后面的#\n",
    "                if j+1 < len(s) and s[j+1] == \"#\":\n",
    "                    temp = s[i:j+1]\n",
    "                    i, j = j+2, j+3\n",
    "                else:\n",
    "                    i, j = j, j+1\n",
    "            else:\n",
    "                i, j = j, j+1\n",
    "            ans.append(word[temp])\n",
    "        return \"\".join(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def helper(ch):\n",
    "            return chr(int(ch) + 96)\n",
    "        ans = ''\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if i + 2 < n and s[i+2] == '#':\n",
    "                ans += helper(s[i:i+2])\n",
    "                i+=3\n",
    "            else :\n",
    "                ans += helper(s[i])\n",
    "                i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i+2] == '#':\n",
    "                ans += get(s[i:i+2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "        i = 0\n",
    "        res = \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2] == '#':\n",
    "                res += get(s[i : i + 2])\n",
    "                i += 2\n",
    "            else:\n",
    "                res += get(s[i])\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        i = len(s) - 1\n",
    "        while i >= 0:\n",
    "            if s[i] == \"#\":\n",
    "                ans += chr(int(s[i - 2: i]) + 96)\n",
    "                i -= 3\n",
    "            else:\n",
    "                ans += chr(int(s[i]) + 96)\n",
    "                i -= 1\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        i = len(s) - 1\n",
    "        while i >= 0:\n",
    "            if s[i] == \"#\":\n",
    "                ans += chr(int(s[i - 2: i]) + 96)\n",
    "                i -= 2\n",
    "            else:\n",
    "                ans += chr(int(s[i]) + 96)\n",
    "            i -= 1\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        ans = ''\n",
    "        key = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        i = n-1\n",
    "        while i>=0:\n",
    "            if s[i] == '#':\n",
    "                i -=2\n",
    "                t = key[int(s[i:i+2])-1]\n",
    "                ans += t\n",
    "            else:\n",
    "                t = key[int(s[i])-1]\n",
    "                ans += t\n",
    "            i -=1\n",
    "\n",
    "        return ans[::-1]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        dic = {'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f','7':'g','8':'h','9':'i','10':'j','11':'k','12':'l','13':'m','14':'n','15':'o','16':'p','17':'q','18':'r','19':'s','20':'t','21':'u','22':'v','23':'w','24':'x','25':'y','26':'z'}\n",
    "        cur = []\n",
    "        i = len(s)-1\n",
    "        while i > -1:\n",
    "            if s[i] == '#':\n",
    "                cur.append(s[i-2:i])\n",
    "                i -= 2\n",
    "            else:\n",
    "                cur.append(s[i])\n",
    "            i -= 1\n",
    "        res = ''\n",
    "        for i in range(len(cur)-1, -1, -1):\n",
    "            res += dic[cur[i]]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        ans = ''\n",
    "        key = ''\n",
    "        i = n-1\n",
    "        while i>=0:\n",
    "\n",
    "            if s[i] == '#':\n",
    "                i -=2\n",
    "\n",
    "                t = chr(int(s[i:i+2])+96)\n",
    "                ans += t\n",
    "            else:\n",
    "                t = chr(int(s[i])+96)\n",
    "                ans += t\n",
    "            i -=1\n",
    "        for i in range(len(ans)-1,-1,-1):\n",
    "            key += ans[i]\n",
    "\n",
    "        return key\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# https://leetcode.cn/problems/decrypt-string-from-alphabet-to-integer-mapping/\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        map = {\n",
    "            '1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i',\n",
    "            '10#':'j', '11#':'k', '12#':'l', '13#':'m', '14#':'n', '15#':'o', '16#':'p', '17#':'q', '18#':'r', '19#':'s',\n",
    "            '20#':'t', '21#':'u', '22#':'v', '23#':'w', '24#': 'x', '25#':'y', '26#':'z',\n",
    "        }\n",
    "        replacemap = {\n",
    "            '10#': 'j', '11#': 'k', '12#': 'l', '13#': 'm', '14#': 'n', '15#': 'o', '16#': 'p', '17#': 'q', '18#': 'r',\n",
    "            '19#': 's',\n",
    "            '20#': 't', '21#': 'u', '22#': 'v', '23#': 'w', '24#': 'x', '25#': 'y', '26#': 'z',\n",
    "        }\n",
    "        for key, val in replacemap.items():\n",
    "            s = s.replace(key, val)\n",
    "        ans = \"\"\n",
    "        for c in s:\n",
    "            if c not in '0123456789':\n",
    "                ans += c\n",
    "            else:\n",
    "                ans += map[c]\n",
    "        return ans\n",
    "\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "#     s = Solution()\n",
    "#     for testcase in [\n",
    "#         \"10#11#12\",\n",
    "#         \"1326#\",\n",
    "#     ]:\n",
    "#         ret = s.freqAlphabets(testcase)\n",
    "#         print(\"testcase={}, ret={}\".format(testcase, ret))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2] == '#':\n",
    "                ans += get(s[i : i + 2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        temp, i, ans = 0, len(s) - 1, \"\"\n",
    "        while i >= 0:\n",
    "            if s[i] == '#':\n",
    "                ans += chr(eval(s[i - 2:i]) + 96)\n",
    "                i -= 3\n",
    "            else:\n",
    "                ans += chr(int(s[i]) + 96)\n",
    "                i -= 1\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        out = ''\n",
    "        idx = len(s)-1\n",
    "        while idx >= 0:\n",
    "            if s[idx] == '#':\n",
    "                out = chr(int(s[idx-2:idx])+96) + out\n",
    "                idx -= 3\n",
    "            else:\n",
    "                out = chr(int(s[idx])+96) + out\n",
    "                idx -= 1\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        x = []\n",
    "        n = len(s)-1\n",
    "        while n > -1:\n",
    "            if s[n] == \"#\":\n",
    "                x.append(chr(96+int(s[n-2:n])))\n",
    "                n -= 3\n",
    "            else:\n",
    "                x.append(chr(96+int(s[n])))\n",
    "                n -= 1\n",
    "        return \"\".join(x[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        return ''.join(chr(int(i[:2]) + 96) for i in re.findall(r'\\d\\d#|\\d', s))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans=''\n",
    "        for i in range(0,len(s)):\n",
    "            if s[i]=='#':\n",
    "                ans=ans[:-2]\n",
    "                ans+=chr(int(s[i-2:i])+96)\n",
    "            else:\n",
    "                ans+=chr(int(s[i])+96)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        #反着遍历\n",
    "        s = s[::-1]\n",
    "        x = \"\"\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] != \"#\":\n",
    "                x += chr(96 + int(s[i]))\n",
    "                i += 1\n",
    "            else:\n",
    "                p = int(s[i + 1 : i + 3][::-1]) + 96\n",
    "                x += chr(p)\n",
    "                i += 3\n",
    "        return x[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def change(a):\n",
    "            return chr(int(a)+96)\n",
    "        i=0\n",
    "        c=''\n",
    "        while i<len(s):\n",
    "            if i+2<len(s) and s[i+2]=='#':\n",
    "                c+=change(s[i:i+2])\n",
    "                i+=2\n",
    "            else:\n",
    "                c+=change(s[i])\n",
    "            i+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        i = n - 1\n",
    "        res = []\n",
    "        while i > -1:\n",
    "            if s[i] == '#':\n",
    "                res.append(chr(int(s[i - 2:i]) + ord('a') - 1))\n",
    "                i -= 3\n",
    "            else:\n",
    "                res.append(chr(int(s[i]) + ord('a') - 1))\n",
    "                i -= 1\n",
    "        return ''.join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st)+96)\n",
    "        i,ans=0,\"\"\n",
    "        while i<len(s):\n",
    "            if i+2<len(s) and s[i+2]=='#':\n",
    "                ans+=get(s[i:i+2])\n",
    "                i+=2\n",
    "            else:\n",
    "                ans+=get(s[i])\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        for i in range(10, 27):\n",
    "            s = s.replace(\"%d#\" %  i, chr(i + 97 - 1))\n",
    "        for i in range(10):\n",
    "            s = s.replace(\"%d\" %  i, chr(i + 97 - 1))\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "        i,ans = 0,\"\"\n",
    "        while i < len(s):\n",
    "            if i+2 < len(s) and s[i+2] == '#':\n",
    "                ans += get(s[i:i+2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def helper(num):\n",
    "            stack = list(num)\n",
    "            ans = \"\"\n",
    "            poped1 = stack.pop()\n",
    "            poped2 = stack.pop() if stack else ''\n",
    "            ans += chr(int(poped2 + poped1) + 96)\n",
    "            if stack:\n",
    "                ans = \"\".join(list(map(lambda x: chr(int(x) + 96), stack))) + ans\n",
    "            return ans\n",
    "#        queue = collections.deque(list(map(lambda x: helper(x), s.split(\"#\"))))\n",
    "\n",
    "        queue = collections.deque(s)\n",
    "        tmp = \"\"\n",
    "        ans = \"\"\n",
    "        while queue:\n",
    "            poped = queue.popleft()\n",
    "            if poped.isdigit():\n",
    "                tmp += poped\n",
    "            else:\n",
    "                ans += helper(tmp)\n",
    "                tmp = \"\"\n",
    "        if tmp:\n",
    "            ans += \"\".join(list(map(lambda x: chr(int(x) + 96), tmp)))\n",
    "        return ans\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans=''\n",
    "        dif=ord('a')-ord('1')\n",
    "        dif1=ord('j')-10\n",
    "        i=0\n",
    "        while i<len(s):\n",
    "            if i<len(s)-2 and s[i+2]=='#':\n",
    "                count=int(s[i])*10+int(s[i+1])\n",
    "                ans+=chr(count+dif1)\n",
    "                i+=3\n",
    "            else:\n",
    "                ans+=chr(ord(s[i])+dif)\n",
    "                i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "\n",
    "        length = len(s)\n",
    "        mystr = ''\n",
    "        strlist = []\n",
    "        dir_str = {'1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f',\n",
    "                '7': 'g', '8': 'h', '9': 'i'}\n",
    "        dir_str2 = {'11#': 'k', '12#': 'l', '13#': 'm', '14#': 'n', '15#': 'o', '16#': 'p',\n",
    "                    '17#': 'q', '18#': 'r', '19#': 's', '20#': 't', '21#': 'u', '22#': 'v',\n",
    "                    '23#': 'w', '24#': 'x', '25#': 'y', '26#': 'z', '10#': 'j'}\n",
    "        i = length-1\n",
    "        while i >= 0:\n",
    "            if s[i] == '#':\n",
    "                strlist.append(dir_str2.get(s[i-2:i+1]))\n",
    "                i -= 3\n",
    "\n",
    "            else:\n",
    "                strlist.append(dir_str.get(s[i]))\n",
    "                i -= 1\n",
    "            \n",
    "        mystr = ''.join(strlist)\n",
    "        mystr = mystr[::-1]\n",
    "        return mystr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def freqAlphabets(self, s: str) -> str:\r\n",
    "        a = [chr(i) for i in range(97,106)]\r\n",
    "        b = [chr(j) for j in range(106,123)]\r\n",
    "        ans = list()\r\n",
    "        i = 0\r\n",
    "        while i < len(s):\r\n",
    "            if i + 2 >= len(s) or s[i+2] != '#':\r\n",
    "                ans.append(a[int(s[i])-1])\r\n",
    "                i += 1\r\n",
    "            else:\r\n",
    "                ans.append(b[int(s[i:i+2])-10])\r\n",
    "                i += 3\r\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        def get(st):\n",
    "            return chr(int(st) + 96)\n",
    "\n",
    "        i, ans = 0, \"\"\n",
    "        while i < len(s):\n",
    "            if i + 2 < len(s) and s[i + 2]== '#':\n",
    "                ans += get(s[i: i+2])\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += get(s[i])\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        ans = \"\"\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if i+2<n and s[i+2] == \"#\":\n",
    "                ans += chr(ord(\"a\")+int(s[i:i+2])-1)\n",
    "                i += 3\n",
    "            else:\n",
    "                ans += chr(ord(\"a\")+int(s[i:i+1])-1)\n",
    "                i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def freqAlphabets(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        i = len(s) - 1\n",
    "        while i >= 0:\n",
    "            if s[i] == \"#\":\n",
    "                ans += chr(int(s[i - 2:i]) + ord(\"a\") - 1)\n",
    "                i -= 3\n",
    "            else:\n",
    "                ans += chr(int(s[i]) + ord(\"a\") - 1)\n",
    "                i -= 1\n",
    "            print(ans)\n",
    "        return ans[::-1]\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
